"use strict";

class Controller {
    constructor (systemService, uiNotification, $modalInstance, statusMap, $location, $stateParams, type, plat,item) {
        "ngInject"
        this.uiModal        = $modalInstance;
        this.type           = type;
        this.isEdit         = false;
        this.notify         = uiNotification;
        this.service        = systemService;
        this.location       = $location;
        this.stateParam     = $stateParams;
        this.stateParam.tab = this.type;
        this.select         = {status: statusMap, plats: plat.data}
        this.item           = angular.copy(item);
        this.data = {
            statusCd: "S0A",
            createDate: + (new Date)
        }
        if(this.item){
            angular.extend(this.data, {
                staffId : this.item.staffId
            })
        }

        switch (type) {
            case 'post' :
                angular.extend( this.data, {
                    sysPostFrom: $stateParams.orgName
                })
                break;
            case 'staff':
                angular.extend(this.data, {
                    staffId : ''
                })
                break;
            case 'user':
                systemService.getPlat().then((res) => {
                    __DEBUG__ && console.log(res)
                    this.select.staffFrom = res.data;
                })
                systemService.getPwdRule('select').then(res => {
                    __DEBUG__ && console.log(res)
                    this.select.strategy = res.data;
                })
                this.data.passNeedModify = true;
                angular.extend(this.data, {
                    staffName: this.item.staffName,
                    password: 111111,
                    pwdErrCnt: 3
                })
                break;
            default:
                break;
        }

        this.data.orgId = $stateParams.id;
    }

    checkValid (field) {
        if ( field.$pristine ) {
            field.$pristine = false;
        }
    }

    save () {

        if ( this.submitting ) return;

        this.submitting = true;

        delete this.data.sysPostFrom;

        this.service
            .insert(this.type, this.data)
            .then(() => {
                this.uiModal.close(this.data);
                //this.location.path('corp/detail/' + this.stateParam.pid + '/' + this.stateParam.id + '/' + this.type + '/' + this.stateParam.name );
                this.notify.success({
                    'title'  : '提示',
                    'message': '操作成功!'
                });
            }).catch((data) => {
                if(data.resultCode==1062){
                    data.resultMsg = '用户账号已存在'
                }
                this.notify.error({
                    'title'  : '提示',
                    'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
                });
            }).finally(() => {
                // 隐藏 loading
                this.submitting = false;
            });

    }

    /*openView(size) {
        /!*let modalInstance = this.uiModal.open({
            animation   : true,
            templateUrl : template,
            controller  : viewController,
            controllerAs: 'vm',
            size        : size || 'md'
        });

        // 处理对话框提交/关闭
        modalInstance.result.then( (res) => {
            if (this.type == "user") {
                angular.extend(this.data, {
                    staffName: res.staffName,
                    staffId  : res.id
                })
            }
        });*!/
    }*/
    cancel() {
        this.uiModal.dismiss('cancel')
    }
}
export default Controller;
